package com.lottery.im.module.result.adapter.result.statistics;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.lottery.im.R;
import java.util.ArrayList;
import java.util.List;

public class HPkAdapter extends BaseAdapter {

  private List<String> items = new ArrayList<>();
  private List<String> mDataList;
  private Context mContext;
  private int index = 0;

  public HPkAdapter(Context context, List<String> list) {
    mDataList = list;
    mContext = context;
    for (int i = 1; i < 11; i++) {
      items.add(i + "");
    }
  }

  public void setIndex(int index) {
    this.index = index;
    notifyDataSetChanged();
  }

  public void setAlpha(List<String> items) {
    this.items = items;
    notifyDataSetChanged();
  }

  @Override public int getCount() {
    return mDataList.size();
  }

  @Override public Object getItem(int position) {
    if (position >= mDataList.size() || position < 0) return null;
    return mDataList.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(final int position, View v, ViewGroup parent) {
    final ViewHolder holder;
    if (v == null) {
      v = LayoutInflater.from(mContext).inflate(R.layout.item_horizontal_pk30_list, parent, false);
      holder = new ViewHolder();
      holder.num = v.findViewById(R.id.tv_num);
      v.setTag(holder);
    } else {
      holder = (ViewHolder) v.getTag();
    }
    String ball = mDataList.get(position);
    switch (index) {
      case 0:
        holder.num.setText(ball);
        holder.num.setBackgroundResource(getResourceId("pk_ball_" + ball));
        if (items.contains(ball)) {
          holder.num.getBackground().setAlpha(255);
        } else {
          holder.num.getBackground().setAlpha(30);
        }
        break;
      case 1:
        if (Integer.parseInt(ball) > 5) {
          holder.num.setText("大");
          holder.num.setBackgroundResource(R.drawable.pk_ball_big);
        } else {
          holder.num.setText("小");
          holder.num.setBackgroundResource(R.drawable.pk_ball_small);
        }
        break;
      case 2:
        if (Integer.parseInt(ball) % 2 == 0) {
          holder.num.setText("双");
          holder.num.setBackgroundResource(R.drawable.pk_ball_big);
        } else {
          holder.num.setText("单");
          holder.num.setBackgroundResource(R.drawable.pk_ball_small);
        }
        break;
      case 3:
        holder.num.setText(ball);
        if ("龙".equals(ball)) {
          holder.num.setBackgroundResource(R.drawable.pk_ball_big);
        } else {
          holder.num.setBackgroundResource(R.drawable.pk_ball_small);
        }
        break;
    }
    return v;
  }

  class ViewHolder {
    protected TextView num;
  }

  //根据图片名称取得对应图片
  public int getResourceId(String name) {
    int resId = mContext.getResources().getIdentifier(name, "drawable", mContext.getPackageName());
    //解析资源文件夹下，id为resId的图片
    return resId;
  }
}